package band.wukong.practice.java.demo;

import java.util.List;
import java.util.ArrayList;
import java.util.Calendar;
import java.io.*;

/**
 * Description: 批量备份数据库
 *
 * 1. 修改数据库链接：ip、username、password
 * 2. 修改输出的文件夹：outputPath
 * 3. 修改getDatabaseList()，加入你的数据库名称
 * 4. 保证你配置了mysql的环境变量
 *
 * @author 胡晓 http://huxiao.javaeye.com/  kskr@qq.com  QQ:376665005
 *
 * 原文链接：http://huxiao.javaeye.com/admin/blogs/604093
 * 转载请保留作者信息
 */
public class BackupMySQL {
    private static String ip = "localhost";
	private static String username = "root";
	private static String password = "root";

	//输出文件夹，我加了一个时间，保证每次生成在不同的文件夹里面，避免多次备份的文件在同一个文件夹里面，影响视听
	private static String outputPath = "g:/backupdatabase/" + getCurrentDate14() + "/";

	/**
	 * <p>功能描述：备份数据库</p>
	 *
	 * @param databaseName
	 * @author 胡晓 <BR> kskr@qq.com <BR>
	 * 时间：Feb 27, 2010 8:34:24 AM <BR>
	 */
	public static void backup(String databaseName) {
		try {
			Process process = Runtime.getRuntime().exec("mysqldump.exe -h" + ip + " -u" + username + " -p" + password + " " + databaseName);

			InputStream input = process.getInputStream();
			InputStreamReader xx = new InputStreamReader(input, "utf8");

			String inputString;
			StringBuffer sb = new StringBuffer("");
			String outputString;
			BufferedReader br = new BufferedReader(xx);
			while ((inputString = br.readLine()) != null) {
				sb.append(inputString + "\r\n");
			}
			outputString = sb.toString();

			FileOutputStream output = new FileOutputStream(outputPath + databaseName + "_" + getCurrentDate14() + ".sql");
			OutputStreamWriter writer = new OutputStreamWriter(output, "utf8");
			writer.write(outputString);
			writer.flush();

			input.close();
			xx.close();
			br.close();
			writer.close();
			output.close();
		} catch (Exception e) {
			System.out.println("备份出错");
			e.printStackTrace();
		}
	}

	/**
	 * <p>功能描述：创建文件夹</p>
	 *
	 * @param path
	 * @author 胡晓 <BR> kskr@qq.com <BR>
	 * 时间：Feb 27, 2010 8:48:27 AM <BR>
	 */
	public static void createFolder(String path) {
		try {
			File file = new File(path);
			if (!file.exists()) {
				file.mkdirs();
			}
		} catch (Exception e) {
			System.out.println("创建文件夹出错");
			e.printStackTrace();
		}
	}

	/**
	 * <p>功能描述：生成之后自动打开所在文件夹</p>
	 *
	 * @param folderPath
	 * @author 胡晓 <BR> kskr@qq.com <BR>
	 * 时间：Feb 27, 2010 8:48:37 AM <BR>
	 */
	public static void openFolder(String folderPath) {
		try {
			// 打开所在文件夹
			String path = folderPath.replaceAll("/", "\\\\");
			String cmd[] = { "explorer.exe", path };
			Runtime.getRuntime().exec(cmd);
		} catch (Exception e) {
			System.out.println("打开文件夹“" + folderPath + "”出错");
			e.printStackTrace();
		}
	}

	/**
	 * <p>功能描述：获取14位的当前时间</p>
	 *
	 * @return
	 * @author 胡晓 <BR> kskr@qq.com <BR>
	 * 时间：Feb 27, 2010 8:47:36 AM <BR>
	 */
	public static Long getCurrentDate14(){
		Calendar calendar = Calendar.getInstance();
		String strYear = String.valueOf(calendar.get(Calendar.YEAR));
		String strMonth = String.valueOf(calendar.get(Calendar.MONTH) + 1);
		String strDate = String.valueOf(calendar.get(Calendar.DATE));
		String srtHours = String.valueOf(calendar.get(Calendar.HOUR_OF_DAY));
		String strMinute = String.valueOf(calendar.get(Calendar.MINUTE));
		String second = String.valueOf(calendar.get(Calendar.SECOND));
		// 整理格式
		strMonth = strMonth.length() < 2 ? "0" + strMonth : strMonth;
		strDate = strDate.length() < 2 ? "0" + strDate : strDate;
		srtHours = srtHours.length() < 2 ? "0" + srtHours : srtHours;
		strMinute = strMinute.length() < 2 ? "0" + strMinute : strMinute;
		second = second.length() < 2 ? "0" + second : second;
		return Long.valueOf(strYear + strMonth + strDate+ srtHours + strMinute + second);
	}

	/**
	 * <p>功能描述：获取数据库列表</p>
	 *
 * 这里的数据库列表实际上是可以从mysql自带的information_schema数据库中的tables表中读出来的，如果你懒得像我这样一一列举，可以采取那种方式
	 * @return
	 * @author 胡晓 <BR> kskr@qq.com <BR>
	 * 时间：Feb 27, 2010 8:48:58 AM <BR>
	 */
	public static List<String> getDatabaseList() {
		List<String> list = new ArrayList<String>();
		list.add("aliwangpu");
		list.add("djwl");
		list.add("dog");
		list.add("dyfy");
		list.add("fydagl");
		list.add("hngk");
		list.add("hxzg");
		list.add("oa");
		list.add("practise");
		list.add("qzjy");
		list.add("salecount");
		list.add("swcm");
		list.add("xszp");
		list.add("yfhj");
		list.add("yfjy");
		return list;
	}

	public static void main(String[] args) {
		createFolder(outputPath);

		for (String databaseName : getDatabaseList()) {
			backup(databaseName);
		}

		openFolder(outputPath);
	}
}
